package com.jd.orm.framework.core.transaction;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 
 * ClassName: JDOrmClassHelper <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason: TODO ADD REASON. <br/> 
 * date: 2014-5-13 上午9:27:46 <br/> 
 * 
 * @author  liubing1@jd.com 
 * @version  
 * @since up JDK 1.5
 */
public class JdOrmClassHelper {
	/**
	 * 
	 * readAnnotationValue: <br/> 
	 * TODO.<br/> 
	 * @author liubing1@jd.com
	 * @param annotation
	 * @param name
	 * @return 
	 * @since JDK 1.5
	 */
	public static Object readAnnotationValue(Annotation annotation, String name) {
		try {
			Method method = annotation.annotationType().getDeclaredMethod(name);
			return method.invoke(annotation);
		} catch (Exception ignore) {
			return null;
		}
	}

	/**
	 * 
	 * isSubclass: <br/> 
	 * TODO.<br/> 
	 * @author liubing1@jd.com
	 * @param thisClass
	 * @param target
	 * @return 
	 * @since JDK 1.5
	 */
	public static boolean isSubclass(Class<?> thisClass, Class<?> target) {
		if (target.isInterface()) {
			return isInterfaceImpl(thisClass, target);
		}
		for (Class<?> x = thisClass; x != null; x = x.getSuperclass()) {
			if (x == target) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * isInterfaceImpl: <br/> 
	 * TODO.<br/> 
	 * @author liubing1@jd.com
	 * @param thisClass
	 * @param targetInterface
	 * @return 
	 * @since JDK 1.5
	 */
	public static boolean isInterfaceImpl(Class<?> thisClass,
			Class<?> targetInterface) {
		for (Class<?> x = thisClass; x != null; x = x.getSuperclass()) {
			Class<?>[] interfaces = x.getInterfaces();
			for (Class<?> i : interfaces) {
				if (i == targetInterface) {
					return true;
				}
				if (isInterfaceImpl(i, targetInterface)) {
					return true;
				}
			}
		}
		return false;
	}
}
